//
//  HamatonThresholdSettingViewController.swift
//  hamaton-app
//
//  Created by intyt on 2024/4/18.
//  阈值设置

import UIKit

class HamatonThresholdSettingViewController: HamatonBaseViewController {
    var thresholdSettingModel: ThresholdSettingModel = ThresholdSettingModel()
    weak var delegate: HamatonThresholdSettingDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .clear
        navigationItem.title = "阈值设置"
        let margTop: Float = 15
        let margRight: Float = -20
        let margLeft: Float = 20
        // 低压
        view.addSubview(diyaLabel)
        view.addSubview(diyaDanweiLabel)
        view.addSubview(diyaTextField)
        view.addSubview(lineView1)
        // 最低压
        view.addSubview(zuidiyaLabel)
        view.addSubview(zuidiyaDanweiLabel)
        view.addSubview(zuidiyaTextField)
        view.addSubview(lineView2)
        // 高压
        view.addSubview(gaoyaLabel)
        view.addSubview(gaoyaDanweiLabel)
        view.addSubview(gaoyaTextField)
        view.addSubview(lineView3)
        // 高温
        view.addSubview(gaowenLabel)
        view.addSubview(gaowenDanweiLabel)
        view.addSubview(gaowenTextField)
        view.addSubview(lineView4)
        // 传感器丢失报警时间
        view.addSubview(tpmsLabel)
        view.addSubview(tpmsDanweiLabel)
        view.addSubview(tpmsTextField)
        view.addSubview(lineView5)
        // 保存
        view.addSubview(saveBtn)
        
        // 低压
        diyaLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        diyaDanweiLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(40)
            make.height.equalTo(20)
        }
        diyaTextField.snp.makeConstraints { make in
            make.centerY.equalTo(diyaLabel.snp.centerY)
            make.right.equalTo(diyaDanweiLabel.snp.left).offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView1.snp.makeConstraints { make in
            make.top.equalTo(diyaLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 最低压
        zuidiyaLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        zuidiyaDanweiLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(40)
            make.height.equalTo(20)
        }
        zuidiyaTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop)
            make.right.equalTo(zuidiyaDanweiLabel.snp.left).offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView2.snp.makeConstraints { make in
            make.top.equalTo(zuidiyaLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 高压
        gaoyaLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView2.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        gaoyaDanweiLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView2.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(40)
            make.height.equalTo(20)
        }
        gaoyaTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView2.snp.bottom).offset(margTop)
            make.right.equalTo(gaoyaDanweiLabel.snp.left).offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView3.snp.makeConstraints { make in
            make.top.equalTo(gaoyaLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 高温
        gaowenLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView3.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(120)
            make.height.equalTo(20)
        }
        gaowenDanweiLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView3.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(40)
            make.height.equalTo(20)
        }
        gaowenTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView3.snp.bottom).offset(margTop)
            make.right.equalTo(gaowenDanweiLabel.snp.left).offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView4.snp.makeConstraints { make in
            make.top.equalTo(gaowenLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 传感器丢失报警时间
        tpmsLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(150)
            make.height.equalTo(20)
        }
        tpmsDanweiLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(40)
            make.height.equalTo(20)
        }
        tpmsTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(margTop)
            make.right.equalTo(tpmsDanweiLabel.snp.left).offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView5.snp.makeConstraints { make in
            make.top.equalTo(tpmsLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 保存
        saveBtn.snp.makeConstraints { make in
            make.top.equalTo(lineView5.snp.bottom).offset(45)
            make.left.equalToSuperview().offset(26)
            make.right.equalToSuperview().offset(-26)
            make.height.equalTo(40)
        }
    }

    // 低压
    private lazy var diyaLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "低压"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var diyaTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(14)
        textField.text = thresholdSettingModel.diyaStr
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    lazy var diyaDanweiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "kpa"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    private lazy var lineView1: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    
    // 最低压
    private lazy var zuidiyaLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "最低压"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var zuidiyaTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(14)
        textField.text = thresholdSettingModel.zuidiyaStr
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    lazy var zuidiyaDanweiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "kpa"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    private lazy var lineView2: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 高压
    private lazy var gaoyaLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "高压"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var gaoyaTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(14)
        textField.text = thresholdSettingModel.gaoyaStr
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    lazy var gaoyaDanweiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "kpa"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    private lazy var lineView3: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 高温
    private lazy var gaowenLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "高温"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var gaowenTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(14)
        textField.text = thresholdSettingModel.gaowenStr
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    lazy var gaowenDanweiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "℃"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    private lazy var lineView4: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 传感器丢失报警时间
    private lazy var tpmsLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "传感器丢失报警时间"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var tpmsTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(14)
        textField.text = thresholdSettingModel.shijianStr
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    lazy var tpmsDanweiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "min"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    private lazy var lineView5: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    
    // 输入框内容改变
    @objc func textFieldDidChange(tf: UITextField) {
        if tf == diyaTextField {
            var tftext = tf.text ?? ""
            thresholdSettingModel.diyaStr = tftext
        } else if tf == zuidiyaTextField {
            var tftext = tf.text ?? ""
            thresholdSettingModel.zuidiyaStr = tftext
        } else if tf == gaoyaTextField {
            var tftext = tf.text ?? ""
            thresholdSettingModel.gaoyaStr = tftext
        } else if tf == gaowenTextField {
            var tftext = tf.text ?? ""
            thresholdSettingModel.gaowenStr = tftext
        } else if tf == tpmsTextField {
            var tftext = tf.text ?? ""
            thresholdSettingModel.shijianStr = tftext
        }
    }
    // 保存设置
    private lazy var saveBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle(NSLocalizedString("保存", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(16, bold: true)
        button.addTarget(self, action: #selector(onClickSaveButton), for: .touchUpInside)
        button.backgroundColor  = Theme.Color.Background.main
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        return button
    }()
    
    @objc func onClickSaveButton() {
        delegate?.saveThresholdSetting(thresholdSettingModel)
        self.view.makeToastActivity(.center)
    }

}
